package leetcode017;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
 * 17. 电话号码的字母组合
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 *
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 */
public class Study017 {

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.isEmpty()){
            return Collections.emptyList();
        }
        String[][] chars = {{"a", "b", "c"},
                            {"d","e","f"},
                            {"g","h","i"},
                            {"j","k","l"},
                            {"m","n","o"},
                            {"p","q","r","s"},
                            {"t","u","v"},
                            {"w","x","y","z"}};
        List<String> res = Collections.singletonList("");
        for(int i = 0; i < digits.length(); i++) {
            String c = digits.substring(i, i + 1);
            res = getString(res, chars[Integer.parseInt(c) - 2]);
        }
        return res;
    }

    private List<String> getString(List<String> list, String[] s) {
        List<String> res = new ArrayList<>();
        for (String str: list) {
            for (String str2 : s) {
                res.add(str + str2);
            }
        }
        return res;
    }
    
    
}
